﻿using System;
using Less.Core;

namespace Less.Core.Model
{
    /// <summary>
    /// A formal definition for an log entry
    /// </summary>
    public class LogEntry : IId
    {
        private string _systemData;
        private string _userName;
        public int Id { get; set; }
        public DateTime Created { get; set; }
        public EntrySeverities Severity { get; set; }
        public EntryCategories Category { get; set; }
        public string Source { get; set; }
        public string Message { get; set; }
        public string SystemData
        {
            get
            {
                _systemData = string.Format("{0}, Mb:{1}", Environment.MachineName, Environment.WorkingSet / 1024 / 1024);
                return _systemData;
            }
            set
            {
                _systemData = value;
            }
        }
        public string UserName
        {
            get
            {
                if (_userName.IsNullOrEmpty())
                    _userName = Environment.UserName;
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        public long ProcessId { get; set; }
        public string MetricName { get; set; }
        public object MetricValue { get; set; }
        public LogEntry()
        {
        }
        public LogEntry(string systemData, string userName, int id, DateTime created, EntrySeverities severity, EntryCategories category, string source,
                        string message, long processId, string metricName, object metricValue)
        {
            SystemData = systemData;
            UserName = userName;
            Id = id;
            Created = created;
            Severity = severity;
            Category = category;
            Source = source;
            Message = message;
            ProcessId = processId;
            MetricName = metricName;
            MetricValue = metricValue;
        }
        /// <summary>
        /// Custom constructor
        /// </summary>
        /// <param name="entrySeverity"></param>
        /// <param name="entryMessage"></param>
        /// <param name="entryCategory"></param>
        /// <param name="entrySource"></param>
        /// <param name="userName"></param>
        /// <param name="processId"></param>
        /// <param name="metricName"></param>
        /// <param name="metricValue"></param>
        /// <returns></returns>
        public static LogEntry CreateLogEntry(EntrySeverities entrySeverity, string entryMessage, EntryCategories entryCategory, string entrySource = "",
                                              string userName = "",
                                              long processId = 0, string metricName = "", object metricValue = null)
        {
            return new LogEntry
            {
                Created = DateTime.Now,
                Severity = entrySeverity,
                Category = entryCategory,
                Message = entryMessage,
                Source = entrySource,
                UserName = userName,
                ProcessId = processId,
                MetricName = metricName,
                MetricValue = metricValue
            };
        }
    }
 }